/**
 * ztree插件的封装
 */
(function () {
    /**
     * 初始化参数
     * @param id 页面的domId
     * @param url 请求数据的地址
     * @param async 是否异步加载
     * @param initId 初始化加载的节点id
     * @param pid 初始化加载的根节点id
     * @param rank 最大加载的树等级
     */
    var $ZTree = function (id, url, async, initId, pid, rank) {
        this.id = id;
        this.url = Feng.ctxPath + url;
        this.onClick = null;
        this.settings = null;
        this.ondblclick = null;
        this.async = async != null ? async : false;
        this.initId = initId != null ? initId : null;
        this.rank = rank;
        this.pid = pid;
        this.url = this.url + "?rank=" + (rank != undefined ? rank : "");
    };

    $ZTree.prototype = {
        /**
         * 初始化ztree的设置
         */
        initSetting: function () {
            var settings = {
                view: {
                    dblClickExpand: true,
                    selectedMulti: false
                },
                async: {
                    enable: this.async,
                    url: this.url,
                    autoParam: ["id=pid"]
                },
                data: {simpleData: {enable: true}},
                callback: {
                    onClick: this.onClick,
                    onDblClick: this.ondblclick
                }
            };
            return settings;
        },

        /**
         * 手动设置ztree的设置
         */
        setSettings: function (val) {
            this.settings = val;
        },

        /**
         * 初始化ztree
         */
        init: function () {
            var zNodeSeting = null;
            if (this.settings != null) {
                zNodeSeting = this.settings;
            } else {
                zNodeSeting = this.initSetting();
            }
            var zNodes = this.loadNodes(this.initId, this.pid);
            $.fn.zTree.init($("#" + this.id), zNodeSeting, zNodes);
            if (this.initId != null && this.initId != "") {
                this.setSelectedVal(this.initId);
            }
        },

        /**
         * 绑定onclick事件
         */
        bindOnClick: function (func) {
            this.onClick = func;
        },
        /**
         * 绑定双击事件
         */
        bindOnDblClick: function (func) {
            this.ondblclick = func;
        },


        /**
         * 初始化时，加载的节点
         * @param initId 初始化，加载到的选中节点
         * @param pid 初始化，从指定父节点加载
         * @returns {*}
         */
        loadNodes: function (initId, pid) {
            var zNodes = null;
            var ajax = new $ax(this.url, function (data) {
                zNodes = data;
            }, function (data) {
                Feng.error("加载ztree信息失败!");
            });
            if (initId != null && initId != undefined) {
                ajax.set("initId", initId);
            }
            if (pid != null && pid != undefined) {
                ajax.set("pid", pid);
            }
            ajax.start();
            return zNodes;
        },

        /**
         * 获取选中的值
         */
        getSelectedVal: function (joinPName) {
            var zTree = $.fn.zTree.getZTreeObj(this.id);
            var nodes = zTree.getSelectedNodes();
            if (joinPName) {
                var nodePath = "";
                $.each(nodes[0].getPath(), function (i, n) {
                    if (n.id != 0) {
                        nodePath += (n.name + "-");
                    }
                });
                return nodePath.slice(0, -1);
            } else {
                return nodes[0].name;
            }
        },

        /**
         * 设置选中的节点
         * @param val
         */
        setSelectedVal: function (val) {
            if (val != null && val != "") {
                var zTree = $.fn.zTree.getZTreeObj(this.id);
                var node = zTree.getNodeByParam("id", val);
                //指定节点未加载
                if (node == null) {
                    this.initId = val;
                    this.init();
                    node = zTree.getNodeByParam("id", val);
                }
                zTree.selectNode(node);
                this.onClick(null, null, node);
            }
        },

        /**
         * 销毁树结构
         */
        destory: function () {
            $.fn.zTree.destroy(this.id);
        }

    };

    window.$ZTree = $ZTree;

}());